function err = swfnorm(W,H)
% calculate Frobenius norm of (W-H H^{T}) (sparse matrix)
% scripted by Sun Sibai
% 2015 Apr. 11
%   Frobenius Norm: ||M||_{F}=sqrt(sum_{j,k}(M_{j,k}^{2}))
%                            =sqrt(tr(M M^{T}))
%   for sparse matrix, H H^{T} will consume too much space.
%   therefore we are about to use this expression:
%   M=W-HH^{T}
%   fnorm(M)^2=tr(MM^{T})=tr(WW^{T})-2tr(H^{T}W^{T}H)+tr((H^{T}H)^2)
%             =fnorm(W)^2-2tr(H^{T}W^{T}H)+fnorm(H^{T}H)^2
 N = size(H,1);
 k = size(H,2);
 err = norm(W,'fro')^2+norm(H'*H,'fro')^2-2*trace(H'*W'*H);
 err = sqrt(err);
end
